<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算属性的 gettter 和 setter 用法</title>
</head>
<body>
<div id="app">
    <!--  blur 监听元素失去焦点事件  -->
    <div>
        姓氏：<input type="text" name="familyName" id="familyName" v-on:keyup.enter="familyNameClicked"
                    v-on:blur="familyNameClicked" v-bind:value="familyName">
    </div>
    <div>
        名称：<input type="text" name="firstName" id="firstName" v-on:keyup.enter="firstNameClicked"
                    v-on:blur="firstNameClicked" v-bind:value="firstName">
    </div>

    <div>
        姓名全名：{{ familyName}} {{ firstName }}
    </div>
    <div>
        姓名全名：{{ fullName() }}
    </div>
    <div>
        姓名全名：{{ getFullName }}
    </div>

    <div>
        姓名：<input type="text" name="name" id="name" v-bind:value="getFullName" v-on:input="onNameChange">
    </div>
</div>
<script src="../../../../../js/vue.global.js"></script>
<script>
    const app = Vue.createApp({
        data() {
            return {
                firstName: null,
                familyName: null,
            }
        },
        methods: {
            familyNameClicked() {
                let family = document.getElementById("familyName")
                this.familyName = family.value
            },
            firstNameClicked() {
                let first = document.getElementById("firstName")
                this.firstName = first.value
            },

            fullName() {
                console.log("调用了 fullName 函数");
                return this.familyName + " " + this.firstName
            },
            onNameChange(){
                let name = document.getElementById("name")
                this.getFullName = name.value
            }
        },
        computed: {
            // getFullName() {
            //     console.log("调用了 getFullName 函数");
            //     return this.familyName + " " + this.firstName
            // }

            getFullName:{
                get:function (){
                    console.log("调用了 getFullName 函数");
                        return this.familyName + " " + this.firstName
                },
                set:function (value) {
                    let names  = value.split(" ");
                    this.familyName = names[0]
                    this.firstName = names[1]
                }
            }
        }
    })

    app.mount("#app")
</script>
</body>
</html>